package cn.jljiayu.business.sys.oauth.controller;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClient;
import org.springframework.security.oauth2.client.annotation.RegisteredOAuth2AuthorizedClient;
import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 2022/5/20 5:29 下午
 *
 * @author zhangby
 **/
@RestController
public class OauthClientController {

    @GetMapping(value = {"/"})
    public String test(@RegisteredOAuth2AuthorizedClient("client1-authorization-code")
                               OAuth2AuthorizedClient authorizedClient) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        return "index";
    }

    @GetMapping(value = "/authorize", params = "grant_type=authorization_code")
    @ResponseBody
    public String authorizationCodeGrant(Model model,
                                         @RegisteredOAuth2AuthorizedClient("client1-authorization-code")
                                                 OAuth2AuthorizedClient authorizedClient) {



        return "authorize";
    }

    @GetMapping(value = "/authorized", params = OAuth2ParameterNames.ERROR)
    @ResponseBody
    public String authorizationFailed(Model model, HttpServletRequest request) {
        return "authorized";
    }
}
